﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OutdoorLib.BaseEntity.Agency;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Filters;
using Grishko.DataTier;
using Grishko.DataTier.SqlSentence;
using System.Web.Services;
using OutdoorLib.BaseEntity.Constraction;
using Grishko.Tools;



public partial class SurfaceGroupEditor : MasterPage_EditControl
{
    [WebMethod]
    public static void SelectSide(string ID)
    {
        string CurrentID = HttpContext.Current.Request.QueryString[GlobalConstants.qID];
        string CurrentTagName = HttpContext.Current.Request.QueryString[GlobalConstants.qID];
        TSurfaceGroup _SurfaceGroup = (TSurfaceGroup)AppEnvironment.ObjectFactory.GetObject(CurrentID, "SurfaceGroup");

        Side Side = (Side)AppEnvironment.ObjectFactory.GetObject(ID, "Side");
        _SurfaceGroup.Items.Add(Side);
        AppEnvironment.ObjectFactory.SaveObject(_SurfaceGroup);
            
    }
    protected void btnSaveExit_Click(object sender, EventArgs e)
    {

        QueryStringTools.RefreshPageWitchReturnUrl();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        MyInit();
        if (!this.IsPostBack)
        {
            //начинаем - только в группе 
            chOnlyGroupSide.Checked = true;
            OnOffOnlySelect(chOnlyGroupSide, null);
            
            MyDataBind();
        }


    

    }
    private void MyInit()
    {
        //ctlFilterEditor1.GetWhere = ctlFilterEditor1_GetListWhere;
        //ctlFilterEditor1.OnImChange = ctlFilterEditor1_OnImChange;
        HideAlert();
        SetAlertForTextBox();
    }

    
    private TSurfaceGroup _SurfaceGroup = null;

    private void MyDataBind()
    {
        lvSideList.DataSource = Source;
        DataBind();
    }
    
    private bool Save()
    {
        try
        {
            // Если эта новая запись - id убрать префикс новой записи
            if (GlobalConstants.IsNewRecord(SurfaceGroup.ID))
                SurfaceGroup.ID = CurrentID;

            AppEnvironment.ObjectFactory.SaveObject(SurfaceGroup);
            // почистить кэш 
            AppEnvironment.ObjectFactory.ResetCashe(GlobalConstants.GetNewID(CurrentID));

            return true;
        }
        catch
        {
            return false;
        }
    }
    
    protected TSurfaceGroup SurfaceGroup
    {
        get
        {
            //ctlFilterEditor1.GetWhere = ctlFilterEditor1_GetListWhere;
            if (null != _SurfaceGroup)
                    return _SurfaceGroup;
                    
            if (GlobalConstants.IsNewRecord(CurrentID))
                _SurfaceGroup = (TSurfaceGroup)AppEnvironment.ObjectFactory.GetNewObject(CurrentTagName, GlobalConstants.GetNewID(CurrentID));
            else
                _SurfaceGroup = (TSurfaceGroup)AppEnvironment.ObjectFactory.GetObject(CurrentID, CurrentTagName);

            return _SurfaceGroup;
        }

    }
    
    protected void ReadForm()
    {
        SurfaceGroup.Name = txtName.Text;
        SurfaceGroup.Describe = txtComment.Text;
    }
    
    protected void btnSaveCommonInfo_Click(object sender, EventArgs e)
    {
        ReadForm();
        Save();
        HideAlert();
        MyDataBind();
        
    }

    List<EntitySingle> _Source = null;
    private List<EntitySingle> Source
    {
        get
        {
            if (null != _Source)
                return _Source;

            if (null == ListFilter)
                return new List<EntitySingle>();

            _Source = ListFilter.GetCollection();
            return _Source;

        }
    }


    SideListFilter _ListFilter = null;
    protected SideListFilter ListFilter
    {
        get{
            if (null != _ListFilter)
                    return _ListFilter;

            string key=CurrentID + "ListFilter";
            if (CacheTools.IsSessionCacheObject(key))
                _ListFilter = (SideListFilter)CacheTools.GetSessionCacheObect(key);
            else
            {
                _ListFilter = new SideListFilter();
                CacheTools.SetSessionCacheObject(key, _ListFilter);
            }
            return _ListFilter;
   
            }

    }

    protected ScriptEngine ctlFilterEditor1_GetListWhere()
    {
        if (null == ListFilter )
            return null;

        return ListFilter.Where;
    }

    protected bool ctlFilterEditor1_OnImChange()
    {
        MyDataBind();
        return true;
    }

    protected bool IsNewRecord
    {
        get { return GlobalConstants.IsNewRecord(CurrentID); }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if(-1==MultiView1.ActiveViewIndex)
                MultiView1.ActiveViewIndex = 0;
        else
            MultiView1.ActiveViewIndex = -1;
    }
    protected void SelectSide_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void OnOffOnlySelect(object sender, EventArgs e)
    {
        CheckBox ch = (CheckBox)sender;
        if (ch.Checked)
        {
            ListFilter.Where.MandatoryConditions.Clear();
            ListFilter.Where.MandatoryConditions.AddMandatoryCondition_Eq("SurfaceGroup.ID", CurrentID);
        }
        else
        {
            ListFilter.Where.MandatoryConditions.Clear();
        }        

        MyDataBind();
    }

    protected bool SideInGroup(string SideID)
    {
        return null != SurfaceGroup.Items[SideID];
    }


    

    protected void SideInGroup_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox b = (CheckBox)sender;
        Func<Side> fSide = delegate()
        {
            foreach (OutdoorLib.BaseEntity.Filters.SideListFilter.SideRow  i in Source)
            {
                if (string.CompareOrdinal(i.ID, b.CssClass) == 0)
                    return i.SideEntity;
            }
            return null;
        };

        Side s = fSide();
        if (SideInGroup(s.ID))
            SurfaceGroup.Items.Remove(s.ID);            
        else
            SurfaceGroup.Items.Add(s);

        AppEnvironment.ObjectFactory.SaveObject(SurfaceGroup);

    }
}
